package org.jwos.platform.service.internal;

import java.util.List;

import org.jwos.platform.dao.UserDAO;
import org.jwos.platform.domain.User;
import org.jwos.platform.domain.UserExistException;
import org.jwos.platform.service.UserManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class StubUserManager implements UserManager {	

	private UserDAO userDAO;
	
	@Autowired
	public void init(UserDAO userDAO) {
		this.userDAO = userDAO;
	}

	@Transactional
	public void saveUser(User user) throws UserExistException {
		if(user.getId()!=null && user.getId().trim().length()>0) {
			User oldUser = this.userDAO.queryUserById(user.getId());
			oldUser.setUserName(user.getUserName());
			oldUser.setEmail(user.getEmail());
			this.userDAO.updateUser(oldUser);
		} else {
			this.userDAO.insertUser(user);
		}		
	}

	@Transactional
	public void deleteUser(String userId) {
		this.userDAO.deleteUser(userId);		
	}

	@Transactional
	public void disableUser(String userId) {
		User user = this.userDAO.queryUserByUserId(userId);
		user.setEnable(false);
		this.userDAO.updateUser(user);
	}

	@Transactional
	public void enableUser(String userId) {
		User user = this.userDAO.queryUserByUserId(userId);
		user.setEnable(true);
		this.userDAO.updateUser(user);
	}

	@Transactional(readOnly=true)
	public User queryUserByUserId(String userId) {
		return this.userDAO.queryUserByUserId(userId);
	}

	@Transactional(readOnly=true)
	public User queryUserEmail(String email) {
		return this.userDAO.queryUserByEmail(email);
	}
	
	@Transactional(readOnly=true)
	public List queryAllUsers() {
		return this.userDAO.queryAllUsers();
	}
}
